package com.flowCloud.config.interceptor;


import com.flowCloud.config.exception.BadRequestParameterException;
import com.flowCloud.config.token.JwtTokenUtil;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @Description: 登录拦截器
* @Param:
* @return:
* @Author: Mr.Yan
* @Date: 2018/12/11
*/

public class SecurityInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object)
            throws Exception {
        // 取得token
        String tokenHeader = request.getHeader(JwtTokenUtil.TOKEN_NAME);
        if (tokenHeader == null) {
            throw new BadRequestParameterException("10210", "token失效,请重新登录！");

        }
        // 验证token是否有效--无效已做异常抛出，由全局异常处理后返回对应信息
        JwtTokenUtil.getTokenBody(tokenHeader);

        return true;
    }
}
